package org.refresh.emf;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.refresh.core.intelligence.ObjectType;

/******************************************************************************
 * Copyright (c) 2005, 2006 Jules White.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Jules White - initial API and implementation 
 ****************************************************************************/
public class EObjectType implements ObjectType {

	private static Map<EClass, EObjectType> typeMap_ = new HashMap<EClass,EObjectType>();
	
	public static EObjectType getType(EClass c){
		EObjectType t = typeMap_.get(c);
		if(t == null){
			t = new EObjectType(c);
			typeMap_.put(c, t);
		}
		return t;
	}
	
	private EClass eClass_;
	
	private EObjectType(EClass class1) {
		super();
		eClass_ = class1;
	}

	public List<ObjectType> getDerivedTypes() {
		return Collections.emptyList();
	}

	public List<ObjectType> getSuperTypes() {
		ArrayList<ObjectType> sups = new ArrayList<ObjectType>();
		for(EClass sup : eClass_.getEAllSuperTypes()){
			sups.add(getType(sup));
		}
		return sups;
	}

	public boolean instanceOf(Object obj) {
		if(obj instanceof EObject){
			EObject eobj = (EObject)obj;
			if(eobj.eClass().equals(eClass_) || eobj.eClass().getEAllSuperTypes().contains(eClass_)){
				return true;
			}
		}
		return false;
	}

	/**
	 * @return
	 * @see org.eclipse.emf.ecore.ENamedElement#getName()
	 */
	public String getName() {
		return eClass_.getName();
	}

}
